package com.tim.ying.tree.util;

import com.tim.ying.tree.node.TreeNode;

/**
 * 树的基础操作
 * @author yingbibo
 *
 */
public class TreeUtils {
	/**
	 * 树高度
	 * @param treeNode
	 * @return
	 */   
	public static int height(TreeNode treeNode){
		if(treeNode==null){
			return 0;
		}else{
			 int leftHeight = height(treeNode.left);
			 int rightHeight = height(treeNode.right);
			 return (leftHeight<rightHeight)?(rightHeight+1):(leftHeight+1);
		}
	}
	/**
	 * 树节点树
	 * @param treeNode
	 * @return
	 */
	public static int size(TreeNode treeNode){
		if(treeNode==null){
			return 0;
		}else{
			int leftSize = size(treeNode.left);
			int rightSize = size(treeNode.right);
			return (leftSize+rightSize+1);
		}
	}
	
	/**
	 * 前序遍历
	 * @param treeNode
	 * 根左右
	 */
	public static void preOrder(TreeNode treeNode){
		if(treeNode==null){
			return ;
		}else{
			System.out.print(treeNode.name+ ",");
			preOrder(treeNode.left);
			preOrder(treeNode.right);
		}
	}
	
	/**
	 * 后续遍历
	 * 左右根
	 * @param treeNode
	 */
	public static void lastOrder(TreeNode treeNode){
		if(treeNode==null){
			return ;
		}else{
			lastOrder(treeNode.left);
			lastOrder(treeNode.right);
			System.out.print(treeNode.name+ ",");
		}
	}
	
	public static void midOrder(TreeNode treeNode){
		if(treeNode==null){
			return ;
		}else{
			lastOrder(treeNode.left);
			System.out.print(treeNode.name+ ",");
			lastOrder(treeNode.right);
		}
	}
}
